package gov.kqqzyyy.monkey.tjpd.ui;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;

import com.facebook.drawee.generic.GenericDraweeHierarchy;
import com.facebook.drawee.generic.GenericDraweeHierarchyBuilder;
import com.facebook.drawee.view.SimpleDraweeView;
import com.socks.library.KLog;

import butterknife.BindView;
import butterknife.ButterKnife;
import gov.kqqzyyy.monkey.tjpd.R;
import gov.kqqzyyy.monkey.tjpd.mytools.MyBaseActivity;
import gov.kqqzyyy.monkey.tjpd.objects.TJ_ZSXX;

public class PptActivity extends MyBaseActivity {

    @BindView(R.id.ppt_draweeview)
    SimpleDraweeView simpleDraweeView;

    private String[] mImgUrlArray;

    private int zsid;
    private int position;

    private CountDownTimer countDownTimer;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_MENU:
                Intent intent = new Intent(getApplicationContext(), FragmentPreferences.class);
                startActivity(intent);
                finish();
                return true;
            case KeyEvent.KEYCODE_BACK:
                finish();
                return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_ppt);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        ButterKnife.bind(this);
        zsid = Integer.valueOf(getMySharedPreferences().getString("clinic_preference", "-1"));
        mImgUrlArray = getResources().getStringArray(R.array.img_url);
        GenericDraweeHierarchy hierarchy = new GenericDraweeHierarchyBuilder(getResources())
                .setFadeDuration(2000)
                .build();
        simpleDraweeView.setHierarchy(hierarchy);

    }

    @Override
    protected void onResume() {
        super.onResume();
        position = 0;
        doGetZsxx(zsid);
        doSetImg();
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (countDownTimer != null) {
            countDownTimer.cancel();
        }
    }

    @Override
    protected void doSetZsxx(TJ_ZSXX tjZsxx) {
        super.doSetZsxx(tjZsxx);
        KLog.d("APE", tjZsxx);
        if (tjZsxx.getSfqy() == 0) {
            timeToGetInfo();
        } else {
            Intent intent = new Intent(getApplicationContext(), MainActivity.class);
            startActivity(intent);
            finish();
        }
    }

    private void timeToGetInfo() {
        CountDownTimer timer = new CountDownTimer(10000, 5000) {
            @Override
            public void onTick(long millisUntilFinished) {

            }

            @Override
            public void onFinish() {
                doGetZsxx(zsid);
            }
        };
        timer.start();
    }

    private void doSetImg() {

        Uri uri = Uri.parse(mImgUrlArray[position]);
        simpleDraweeView.setImageURI(uri);
        if (countDownTimer != null) {
            countDownTimer.cancel();
            countDownTimer = null;
        }
        countDownTimer = new CountDownTimer(10000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
            }

            @Override
            public void onFinish() {
                if (position < mImgUrlArray.length - 1) {
                    position++;
                } else {
                    position = 0;
                }
                doSetImg();
            }
        };
        countDownTimer.start();
    }

}
